package leetcode_1_200;

import node.ListNode;

import java.util.ArrayList;
import java.util.List;

public class LeeCode_143 {
    public static void main(String[] args) {
        reorderList(new ListNode(1,new ListNode(2,new ListNode(3,new ListNode(4,new ListNode(5))))));
    }
    private static void reorderList(ListNode head) {
        List<ListNode> list = new ArrayList<>();
        while (head != null){
            list.add(head);
            head = head.next;
        }
        if (list.size() == 1 || list.size() == 0)
            return;
        int n = list.size();
        int l = 0, r = n - 1;
        ListNode pre = list.get(l);
        while (l < r){
            if (l != 0)
                pre.next = list.get(l);
            list.get(l).next = list.get(r);
            pre = list.get(r);
            l++;
            r--;
        }
        if (l == r) {
            list.get(l).next = null;
            pre.next = list.get(l);
        }
        else
            pre.next = null;
    }
}
